package com.minelx.sequence.pattern;

import com.minelx.sequence.core.*;

import java.util.List;
import java.util.Map;

public class EnumeratePatternMatcher implements ReturnMatcher<ReturnMatcher<?, StringScanner>, StringScanner> {

	private final JoinMatcher<ReturnMatcher<?, StringScanner>> enumerationsMatcher;

	public EnumeratePatternMatcher(ReturnMatcher<ReturnMatcher<?, StringScanner>, StringScanner> unitMatcher) {
		enumerationsMatcher = new JoinMatcher<>(Match.content(", "), unitMatcher);
	}

	@Override
	public boolean isMatch(StringScanner clue) {
		return clue.exists("{");
	}

	@Override
	public ReturnMatcher<?, StringScanner> onMatched(StringScanner clue) {
		clue.goes("{");
		List<ReturnMatcher<?, StringScanner>> returnMatchers = enumerationsMatcher.get(clue).orElseThrow(RuntimeException::new);
		clue.goes("}");
		return OrMatcher.create(returnMatchers);
	}
}
